package hu.edudroid.sipguide;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.util.Enumeration;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipManager;
import android.net.sip.SipProfile;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ClientActivity extends Activity {

	public SipManager mSipManager = null;
	public SipProfile mSipProfile = null;
	public SipAudioCall.Listener listener = null;
	public SipAudioCall call = null;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Log.e("BEGIN","BEGIN");
		
		Button con = (Button) findViewById(R.id.con);
		con.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "connect", Toast.LENGTH_SHORT).show();
				Log.e("BEGIN","CONNECT");
				try {
					call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:server@192.168.43.1", listener, 30);
					//call = mSipManager.makeAudioCall(mSipProfile.getUriString(), "sip:ghingis@192.168.1.207", listener, 30);
				} catch (SipException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		});

		if (mSipManager == null) {
			mSipManager = SipManager.newInstance(this);
		}
		
		
		
		SipProfile.Builder builder = null;
		try {
			builder = new SipProfile.Builder("sip:client@"+getLocalIpAddress());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		mSipProfile = builder.build();
		
		Intent intent = new Intent();
		intent.setAction("RIBANCKURVA");
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);
		try {
			mSipManager.open(mSipProfile, pendingIntent, null);
			Log.e("asd","");
		} catch (SipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.e("Intent","CONNECT");
		
		Button disc = (Button) findViewById(R.id.disc);
		disc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				    if (mSipManager == null) {
				       return;
				    }
				    try {
				       if (mSipProfile != null) {
				    	  call.endCall();
				    	  call.close();
				          //mSipManager.close(mSipProfile.getUriString());
				       }
				     } catch (Exception ee) {
				       Log.d("WalkieTalkieActivity/onDestroy", "Failed to close local profile.", ee);
				     }
				}
			
		});
		
		
		listener = new SipAudioCall.Listener() {
			  
			   @Override
			   public void onCallEstablished(SipAudioCall call) {
			      call.startAudio();
			      //call.setSpeakerMode(true);
			      //call.toggleMute();
			      
			   }
			   
			   @Override
			   public void onCallEnded(SipAudioCall call) {
				   Toast.makeText(getApplicationContext(), "CallEnded", Toast.LENGTH_SHORT).show();
			   }
			};
		
		

	}
	
	public String getLocalIpAddress() {
	    try {
	        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
	            NetworkInterface intf = en.nextElement();
	            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
	                InetAddress inetAddress = enumIpAddr.nextElement();
	                if (!inetAddress.isLoopbackAddress()) {
	                    return inetAddress.getHostAddress().toString();
	                }
	            }
	        }
	    } catch (SocketException ex) {
	        Log.e("", ex.toString());
	    }
	    return null;
	}
	
	
	@Override
    public void onDestroy() {
        super.onDestroy();
        if (call != null) {
            call.close();
        }
	}
}